package com.cxmx.escort.controller;

import com.cxmx.escort.base.BaseController;
import com.cxmx.escort.common.codec.StringUtils;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@EnableAutoConfiguration
@RequestMapping(value = "/nbiot/oceanconnect")
public class OceanConnectAction extends BaseController {

//    {notifyType=deviceDataChanged,
//    deviceId=7a0b8d10-7ad0-4078-93ec-38d3a3a270b6,
//    gatewayId=7a0b8d10-7ad0-4078-93ec-38d3a3a270b6,
//    requestId=null, service={serviceId=testFuWu,
//    serviceType=testFuWu, data={openDoor=3836313432383034303030373131350012340001016163000000000000000000000000}, eventTime=20190707T125606Z}}
//imei deviceId
//cmd openDoor
//serviceId serviceId
//openDOOR 70个字符 35个字节 取前面30个字符 16字符
//3836313432383034303031393937390012340001000A63000000000000000000000000

//    @Autowired
//    RedisService redisService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping(value = "/receive", method = RequestMethod.POST)
    @ResponseBody
    public void receive(@RequestBody String body) {
        logger.error("data receive: content --- " + body);
        Gson gson = new Gson();
        Map tmp = gson.fromJson(body, Map.class);
        if (tmp != null && tmp.containsKey("notifyType") && tmp.containsKey("deviceId")) {
            logger.error("here 1");
            String notifyType = tmp.get("notifyType").toString();
            String deviceId = tmp.get("deviceId").toString();
            if (notifyType.equalsIgnoreCase("deviceDataChanged") && tmp.containsKey("service")) {
                logger.error("here 2");
                Map<String, Object> service = (Map<String, Object>) tmp.get("service");
                if (service != null && service.containsKey("serviceId") && service.containsKey("data")) {
                    logger.error("here 3");
                    String serviceId = service.get("serviceId").toString();
                    Map<String, Object> data = (Map<String, Object>) service.get("data");
                    if (data != null && data.containsKey("openDoor")) {
                        logger.error("here 4");
                        String cmd = data.get("openDoor").toString();
                        try {
                            if (StringUtils.isNotEmpty(cmd) && cmd.length() >= 32) {
                                //指定类型 信号值 电量 锁编码
                                String type = cmd.substring(30, 32);
                                logger.error("here 5,type " + type);
                                /*/**
                                 *00 按键上报(请求开锁)
                                 *01 心跳包
                                 *02 开锁成功
                                 *03 开锁失败
                                 *04 关锁成功
                                 *05 关锁失败
                                 */

                                if (type.equalsIgnoreCase("00")) {
                                    bedService.requestOpenLock(deviceId, cmd);
                                }
                                if (type.equalsIgnoreCase("01")) {
                                    bedService.requestSynchronous(deviceId, cmd);
                                }
                                if (type.equalsIgnoreCase("02")) {
                                    bedService.requestOpenLockSuccess(cmd);
                                }
                                if (type.equalsIgnoreCase("03")) {
                                }
                                if (type.equalsIgnoreCase("04")) {
                                    bedService.requestCloseLockSuccess(cmd);
                                }
                                if (type.equalsIgnoreCase("05")) {
                                }
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }

                    }
                }
            }
        }

    }

}


